package com.hwh.blog.web;

import com.hwh.blog.po.Blog;
import com.hwh.blog.po.Comment;
import com.hwh.blog.service.BlogServiceImpl;
import com.hwh.blog.service.CommentService;
import com.hwh.blog.service.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CommentController {
    @Autowired
    CommentServiceImpl commentService;
    @Autowired
    BlogServiceImpl blogService;
    @Value("${comment.avatar}")
    String avatar;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        model.addAttribute("comments", commentService.listComment(blogId));
        return "blog :: commentList";
    }
    @RequestMapping("/comments")
    public String Post(Comment comment){
        Long blogID = comment.getBlog().getId();
        comment.setBlog(blogService.getBlog(blogID));
        comment.setAvatar(avatar);
        commentService.saveComment(comment);
        return "redirect:/comments/"+blogID;
    }
}
